﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HardwareAs.Account
{
    public partial class Login : System.Web.UI.Page
    {
        int isAdmin;
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            // if is valid declare Sesionusername
            //otherwise do nothing
            if (IsValid)
            {
               
                    Session["username"] = UserName.Text.Trim();
                    if (isAdmin == 0)
                    {
                        Response.Redirect("~/Products.aspx");
                    }
                    if (isAdmin == 1)
                    {
                        Session["admin"] = 1;
                        Response.Redirect("~/admin/ManageProduct.aspx");
                    }
              
            }

        }

        protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
        {
            
        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            SqlRequest dbo = new SqlRequest();
            //Check user email and password then return boolean to Valid
           args.IsValid=dbo.CheckUser(UserName.Text.Trim(), Password.Text.Trim(),out isAdmin);
        
               
                
        }
    }
}
